package cn.iocoder.yudao.module.member.vip.controller.admin;

import cn.iocoder.yudao.framework.business.basic.controller.DetailController;
import cn.iocoder.yudao.framework.business.basic.controller.PageController;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.member.vip.dto.AdminVipUserQueryDTO;
import cn.iocoder.yudao.module.member.vip.service.AdminVipUserService;
import cn.iocoder.yudao.module.member.vip.vo.AdminVipUserVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 会员信息")
@RestController
@RequestMapping("/member/vip")
@Validated
public class VipUserController implements DetailController<AdminVipUserVO, Long>, PageController<AdminVipUserVO, AdminVipUserQueryDTO> {
    @Resource
    private AdminVipUserService adminVipUserService;

    @Resource
    private ShopInfoApi shopInfoApi;

    @PreAuthorize("@ss.hasPermission('member:vip:query')")
    @Override
    public CommonResult<AdminVipUserVO> getDetail(Long id) {
        return success(adminVipUserService.getDetail(id));
    }

    @PreAuthorize("@ss.hasPermission('member:vip:query')")
    @Override
    public CommonResult<PageResult<AdminVipUserVO>> getPage(Integer pageNo, Integer pageSize, AdminVipUserQueryDTO param) {
        PageResult<AdminVipUserVO> page = adminVipUserService.getPage(pageNo, pageSize, param);
        Set<Long> shopIds = convertSet(page.getList(), AdminVipUserVO::getBindShopId);
        Map<Long, ShopInfoDTO> shops = shopInfoApi.getShopInfoMap(shopIds);
        for (AdminVipUserVO item : page.getList()) {
            item.setBindShop(shops.getOrDefault(item.getBindShopId(), ShopInfoDTO.FALLBACK));
        }
        return success(page);
    }

}